package com.vitaf.cash.app.weight.input

import android.content.Context
import android.graphics.Typeface
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.View
import androidx.appcompat.widget.AppCompatEditText
import androidx.core.content.res.ResourcesCompat
import com.vitaf.cash.app.R

/**
 * CustomEditText
 * @Author wwdeng
 * @CreateTime: 2024/11/28 14:50
 * @Annotation：自定义输入框，监听焦点变化并上报
 */
class RobotoMediumInputView : AppCompatEditText, TextWatcher, View.OnFocusChangeListener {
    // 是否触发粘贴事件
    private var isPaste = false

    // 是否在未触发粘贴事件的情况下触发了编辑事件或者粘贴后修改了输入文案
    private var isEdit = false

    // 记录触发粘贴事件后第一次的编辑事件
    private var afterPaste = false

    private var startTime: Long = 0
    private var endTime: Long = 0

    //失去焦点回调
    private var mFocusListener: IInputFocusChangeListener? = null

    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init()
    }

    override fun setTypeface(tf: Typeface?) {
        super.setTypeface(tf)
    }

    override fun setTypeface(tf: Typeface?, style: Int) {
        super.setTypeface(ResourcesCompat.getFont(context, R.font.roboto_medium))
    }

    private fun init() {
        setFocusable(true)
        isFocusableInTouchMode = true
        onFocusChangeListener = this
        addTextChangedListener(this)
    }

    override fun onTextContextMenuItem(id: Int): Boolean {
        if (id == android.R.id.paste) {
            isPaste = true
            afterPaste = true
            // 粘贴后重置编辑事件
            isEdit = false
        }
        return super.onTextContextMenuItem(id)
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
    }

    override fun afterTextChanged(s: Editable?) {
        // 粘贴后会同时触发一次afterTextChanged，此时不记录编辑事件
        if (afterPaste) afterPaste = false
        else isEdit = true
    }

    override fun onFocusChange(v: View?, hasFocus: Boolean) {
        this.mFocusListener?.onFocusChange(v, hasFocus)
        if (hasFocus) {
            startTime = System.currentTimeMillis()
        } else {
            endTime = System.currentTimeMillis()
            val content = getText().toString()
            val time = endTime - startTime
            val pe = kotlin.text.StringBuilder()
            if (isPaste) pe.append("P")
            if (isEdit) pe.append("E")
            val remark = "$content$$$time$$$pe"
            // 上报埋点
            this.mFocusListener?.onCompleteAction(v, remark)
        }
    }

    fun setInputFocusChangeListener(listener: IInputFocusChangeListener?) {
        this.mFocusListener = listener
    }

    interface IInputFocusChangeListener {
        fun onFocusChange(v: View?, hasFocus: Boolean)
        fun onCompleteAction(v: View?, remark: String?)
    }
}